import dayjs from "dayjs";
import { MD5 } from "./Md5";

export default class Fn {
    static safeJson(obj: string) {
        try {
            if (typeof obj == "string") {
                if (obj.length > 0 && (obj[0] == "[" || obj[0] == "{")) {
                    return JSON.parse(obj);
                }
                return obj;
            }
            return obj
        } catch (e) {
            return obj;
        }
    }

    static md5(str: string): string {
        return MD5.md5(str);
    }

    static getCurDate(dateStr: any = null) {
        if (dateStr == null) {
            return dayjs();
        }
        if (typeof dateStr == "number") {
            if (dateStr.toString().length > 10) {
                return dayjs(dateStr / 1000);
            }
            return dayjs(dateStr);
        }
        return dayjs(dateStr);
    }
}